<?php

if(!defined('IN_JYCMS')) {
	exit('Access Denied');
}

/**
 * 检测是否需要重新读取文章
 *
 * @param array $news 原文章数组
 * @return array
 */
function focus_check($news) {
	global $_G, $mainframe, $cnsp;
	$changed = false;
	if($news['pageorder']>1) return $news;

	//内容太短，强制更新
	if (strlen($news ['content']) < 100 && $news['readed']<2 && $news['fromurl']) {
		$_GET ['upnews'] = 1;
		//$news ['fromrss'] = 1;
		//$news ['readed'] = 0;
	}

	if ($_GET ['upnews'] == 1 || (strlen($news['conetnt'])<100&&$news ['readed'] < 2 && $news ['fromrss'] == 1 && $news ['fromurl'] != '')) {
		$iconfig = unserialize($_G ['setting'] ['importconfig']);
		require_once libfile('function/focus');
		require_once libfile('class/import');

		$im = new import ();
		$url = $news ['fromurl'];
		//$url = 'http://news.bitauto.com/jw/20100913/0905213760.html';
		if ($row = getrule($url)) {
			$news ['fromrss'] = 0;
			$webname = $row ['title'];
		} else {
			$webname = '网络文摘';
		}
		$news ['readed']++;
		DB::update('portal_article_title', array('readed' => $news['readed']), array('aid' => $news ['aid']));
		$row ['format'] = 'http';
		$row ['url'] = $url;
		$message = $im->geturl($row);

		if ($message && strlen($message) > 200) {
			$message = explode('###NextPage###', $message);
			//pre($message);exit;
			$content_count = count($message);
			$news['contents'] = $content_count;
			//因为可能会有多页采集，所以直接删除
			$aid = $news['aid'];
			//if($content_count<$news['contents']){
			//如果本次采集的页数与上一次采集的页数不同，则删除一下原来超出页面的记录
			$sql = "DELETE FROM #__portal_article_content WHERE aid='$aid'";
			DB::Query($sql);
			//}
			$changed = false;
			$pageorder = 1;
			foreach ($message as $value) {
				$value = trim($value);
				//echo getstr($value,200, 0, 0, 0, -1).'<hr>';
				$setarr = array(
					'aid' => $aid,
					'content' => $value,
					'pageorder' => $pageorder,
					'dateline' => $_G['timestamp']
				);
				if ($iconfig ['saveimg']) {
					$setarr['content'] = processpic($setarr, $url);
				}
				//echo getstr($setarr['content'], 200, 0, 0, 0, -1).'<h2>';
				if ($pageorder == 1) {
					$news['content'] = $setarr['content'];
				}
				$pageorder++;
				DB::Insert('portal_article_content', $setarr);
			}
			if (!$news['keywords']) {
				if (!isset($cnsp)) {
					require_once libfile('class/splitword');
					$cnsp = new SplitWord ();
				}
				$cnsp->SetSource($news ['content']);
				$cnsp->StartAnalysis();
				$news ['keywords'] = $cnsp->GetFinallyIndex(10, ',');
			}
			$news ['newsfrom'] = $webname;
			$data = array();
			$data ['from'] = $webname;
			$data ['fromrss'] = $news ['fromrss'];
			$data ['readed'] = $news ['readed'];
			$data ['keywords'] = $news ['keywords'];
			$data['contents'] = $news['contents'];
			if (!$news ['summary'])
				$data ['summary'] = getstr($news['content'], 150, 0, 0, 0, -1);
			DB::update('portal_article_title', $data, array('aid' => $news ['aid']));
			//发送一条通知，让系统在结束后删除所有采集时生成的临时文件
			$im->finshed = true;
		}
	}
	if($changed){
		//$news['content'] = daddslashes($news['content']);
		DB::update('portal_article_content', array('content'=>daddslashes($news['content'])), array('aid' => $news['aid']));
	}
	return $news;
}

function savedata($row) {
	$v = array('catid', 'title', 'author', 'from', 'fromurl', 'summary', 'kid', 'uid');
	if (!$row ['uid'])
		$row ['uid'] = 1;
	$data ['username'] = $row ['author'];
	$data ['dateline'] = TIMESTAMP;
	$data ['allowcomment'] = $data ['contents'] = 1;
	foreach ($v as $key) {
		$data [$key] = $row [$key];
	}
	$data ['summary'] = cutstr($data ['summary'], 200);
	$aid = DB::Insert('portal_article_title', $data, 1);
	DB::query("UPDATE #__common_category SET articles=articles+1 WHERE catid = '$data[catid]'");
	return $aid;
}

function focuscensor($message, $modword = NULL) {
	if (!class_exists('cmsCensor')) {
		include libfile('class/censor');
	}
	$censor = cmsCensor::instance ();
	$censor->check($message, $modword);
	if ($censor->modbanned()) {
		return false;
	}
	return $message;
}

function processrule($rule) {
	$rule = str_replace("/", '\/', $rule);
	$rule = str_replace("#", '\#', $rule);
	$rule = str_replace("(", '\(', $rule);
	$rule = str_replace(")", '\)', $rule);
	$rule = str_replace("?", '\?', $rule);
	return $rule;
}

function getrule($url) {
	global $_G;
	loadcache('contentrule');
	$contentrule = $_G['cache']['contentrule'];
	if ($contentrule) {
		$find = array();
		foreach ($contentrule as $k => $v) {
			if ($url != str_replace($v ['url'], '', $url)) {
				if (strlen($v ['url']) > strlen($find ['url'])) {
					$find = $v;
				}
			}
		}
		if (count($find) > 0)
			return $find;
		else
			return false;
	}
	return false;
}

function processrepl($rule, $html) {
	$replace = trim($rule);
	if ($replace) {
		$replace = explode("<==newline==>", $replace);
		$replaces = array();
		foreach ($replace as $v) {
			$v = explode("<==>", $v);
			$v [1] = trim($v [1]);
			if ($v [1] == 'BR') {
				$v [1] == '\n';
				$html = preg_replace("/" . trim($v [0]) . "/isU", "\n", $html);
			} else {
				$html = preg_replace("/" . trim($v [0]) . "/isU", trim($v [1]), $html);
			}
		}
	}
	return $html;
}

function clearhtml($string) {
	if (is_array($string)) {
		foreach ($string as $key => $val) {
			$string [$key] = clearhtml($val);
		}
	} else {
		$string = preg_replace('/\<.+?\>/', '', $string);
		$string = preg_replace('/\&([a-zA-Z]+)\;/', '', $string);
	}
	return $string;
}

function filter($html, $vartype = "", $density = false, &$syserror = null) {
	global $cnsp, $_G;
	static $keyword_list;
	$html = trim($html);
	$html = strip_selected_tags($html, "<script><iframe><style><link><meta>");
	$html = remove_xss($html);
	$noerr = true;
	$iconfig = unserialize($_G ['setting'] ['importconfig']);

	$htmlx = diconv($html, CHARSET, 'GB2312');
	$strlen = strlen(preg_replace("/[\x7f-\xff][\x7f-\xff]/", "", $htmlx));
	$cn_len = (strlen($htmlx) - $strlen) / 2;
	preg_match_all("/\\b[a-z]+\\b/i", strip_tags($htmlx), $matches);
	$en_len = sizeof($matches [0]);
	$strlen = strlen($htmlx);

	if ($vartype == 'title') {
		$noerr = true;
		$minlen = intval($iconfig ['len_min']);
		if (0 < $minlen) {
			$noerr = $minlen <= $strlen;
		}
		$maxlen = intval($iconfig ['len_max']);
		if ($maxlen && $noerr) {
			$noerr = $strlen <= $maxlen;
		}
		if (!$noerr) {
			$syserror ['filter_type'] = "len";
			$syserror ['title_length'] = $strlen;
			$syserror ['title_cn'] = $cn_len;
			$syserror ['title_en'] = $en_len;
			return "标题长度不符合要求($strlen)";
		}
	} else if ($vartype == 'newscontent') {
		$noenerr = $nocnerr = true;
		$cnlen = (integer) $iconfig ['cn_min'];
		$enlen = (integer) $iconfig ['en_min'];
		if (0 < $cnlen) {
			$nocnerr = $cnlen < $cn_len;
		}
		if (0 < $enlen) {
			$noenerr = $enlen < $en_len;
		}
		if ($noenerr == false && $nocnerr == false) {
			$syserror ['cnminlen'] = $cnlen;
			$syserror ['enminlen'] = $enlen;
			$syserror ['filter_type'] = "length";
			return "内容长度过短(中文：$cn_len,英文：$en_len)";
		}
	}
	if ($vartype == 'title')
		return false;
	if ($density == true && $iconfig ['density'] != 100 && $iconfig ['density'] != 0) {
		if (!isset($cnsp)) {
			require_once libfile('class/splitword');
			$cnsp = new SplitWord ();
		}
		$cnsp->SetSource($html);
		$cnsp->StartAnalysis();
		$keywords = $cnsp->GetFinallyIndex(1);
		$density = 0;
		if ($keywords) {
			$keyword = $keywords [0] ['word'];
			$density = $keywords [0] ['density'];
		}

		if ($iconfig ['density'] <= $density) {
			$syserror ['filter_type'] = "density";
			return "关键词《" . $syserror ['density'] ['word'] . "》密度(" . $density . ")超过" . $iconfig ['density'] . "，已被过滤。";
		}
	}
	return false;
}

function strip_selected_tags(&$html, $tags = "<script><iframe><style><link>") {
	$tags = trim(str_replace(array(">", "<"), array("", "|"), $tags), "|");
	$html = str_replace(array("&lt;", "&gt;"), array("<", ">"), $html);
	$html = preg_replace("~<(" . $tags . ")[^>]*>(.*?<\\s*\\/(\\1)[^>]*>)?~is", "", $html);
	return $html;
}

function remove_xss($val) {
	$val = preg_replace("/([\\x00-\\x08,\\x0b-\\x0c,\\x0e-\\x19])/", "", $val);
	$zfc = "abcdefghijklmnopqrstuvwxyz";
	$zfc .= "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
	$zfc .= "1234567890!@#\$%^&*()";
	$zfc .= "~`\";:?+/={}[]-_|'\\";
	for ($i = 0; $i < strlen($zfc); ++$i) {
		$val = preg_replace("/(&#[xX]0{0,8}" . dechex(ord($zfc [$i])) . ";?)/i", $zfc [$i], $val);
		$val = preg_replace("/(&#0{0,8}" . ord($zfc [$i]) . ";?)/", $zfc [$i], $val);
	}
	$obj = Array('javascript', 'vbscript', 'expression', 'applet', 'meta', 'xml', 'blink', 'link', 'script', 'embed', 'object', 'iframe', 'frame', 'frameset', 'ilayer', 'layer', 'bgsound', 'base');
	$act = Array('onabort', 'onactivate', 'onafterprint', 'onafterupdate', 'onbeforeactivate', 'onbeforecopy', 'onbeforecut', 'onbeforedeactivate', 'onbeforeeditfocus', 'onbeforepaste', 'onbeforeprint', 'onbeforeunload', 'onbeforeupdate', 'onblur', 'onbounce', 'oncellchange', 'onchange', 'onclick', 'oncontextmenu', 'oncontrolselect', 'oncopy', 'oncut', 'ondataavailable', 'ondatasetchanged', 'ondatasetcomplete', 'ondblclick', 'ondeactivate', 'ondrag', 'ondragend', 'ondragenter', 'ondragleave', 'ondragover', 'ondragstart', 'ondrop', 'onerror', 'onerrorupdate', 'onfilterchange', 'onfinish', 'onfocus', 'onfocusin', 'onfocusout', 'onhelp', 'onkeydown', 'onkeypress', 'onkeyup', 'onlayoutcomplete', 'onload', 'onlosecapture', 'onmousedown', 'onmouseenter', 'onmouseleave', 'onmousemove', 'onmouseout', 'onmouseover', 'onmouseup', 'onmousewheel', 'onmove', 'onmoveend', 'onmovestart', 'onpaste', 'onpropertychange', 'onreadystatechange', 'onreset', 'onresize', 'onresizeend', 'onresizestart', 'onrowenter', 'onrowexit', 'onrowsdelete', 'onrowsinserted', 'onscroll', 'onselect', 'onselectionchange', 'onselectstart', 'onstart', 'onstop', 'onsubmit', 'onunload');
	$objact = array_merge($obj, $act);
	$final = true;
	while ($final == true) {
		$result = $val;
		for ($i = 0; $i < sizeof($objact); ++$i) {
			$regex = "/";
			for ($j = 0; $j < strlen($objact [$i]); ++$j) {
				if (0 < $j) {
					$regex .= "(";
					$regex .= "(&#[xX]0{0,8}([9ab]);)";
					$regex .= "|";
					$regex .= "|(&#0{0,8}([9|10|13]);)";
					$regex .= ")*";
				}
				$regex .= $objact [$i] [$j];
			}
			$regex .= "/i";
			$replace = substr($objact [$i], 0, 2) . "<x>" . substr($objact [$i], 2);
			$val = preg_replace($regex, $replace, $val);
			if ($result == $val) {
				$final = false;
			}
		}
	}
	return $val;
}

function processpic($msgarr = array(), $fromurl = '') {
	$content = preg_replace("/([\/][\s][\t]+)>/i", ">", $msgarr['content']);
	preg_match_all("/<img([^>]+)>/isU", $content, $imglist);
	if (!$imglist[1])
		return $content;
	$iii = array();
	foreach ($imglist[1] as $k => $v) {
		$alt = explode(" ", trim($v));
		$imgart = array();
		foreach ($alt as $i) {
			$i = explode("=", trim($i));
			for ($kk = 2; $kk < count($i); $kk++) {
				if (isset($i[$kk])) {
					$i[1] .= "=" . $i[$kk];
				}
			}
			$imgart[$i[0]] = $i[1];
		}

		if (preg_match("/display[\:\s]+none/iU", $imgart[$i[0]], $tmp)) {
			$content = str_replace("<img" . $v . ">", '', $content);
		} else if (isset($imgart['width']) && $imgart['width'] < 100) {
			$content = str_replace("<img" . $v . ">", '', $content);
		} else if (isset($imgart['height']) && $imgart['height'] < 100) {
			$content = str_replace("<img" . $v . ">", '', $content);
		} else {
			$src = trim($imgart['src']);
			while (substr($src, - 1) == "/") {
				$src = trim(substr($src, 0, - 1));
			}
			if (!$src) {
				$content = str_replace("<img" . $v . ">", '', $content);
			} else {
				$src = str_replace(array("'", '"'), '', $i[1]);
				if (strlen($src) > 20 && substr($src, 0, 8) != 'http:///') {
					$bn = basename($src);
					if (!strlen(fileext($bn)))
						$src .= ".jpg";
					$content = str_replace("<img" . $v . ">", "<img src=" . $src . ">", $content);
					$picarr[$src] = $src;
				}
			}
		}
	}
	$content = preg_replace("/<p><img src=><\/p>/i", '', $content);
	if (!$picarr)
		return $content;
	$url = array();
	foreach ($picarr as $pickey => $picurl) {
		if (!empty($picurl)) {
			$data = array('url' => $picurl, 'from' => $fromurl, 'aid' => $msgarr['aid'], 'pageorder' => $msgarr['pageorder']);
			$picurl = $_G['siteurl'] . 'index.php?option=focus&task=img&data=' . urlencode(compress(serialize($data)));
			$content = str_replace($pickey, $picurl, $content);
		}
	}
	$content = preg_replace("/([a-zA-Z])\/\"/i", '\\1\"', $content);
	$content = preg_replace("/([a-zA-Z])\/\>/i", '\\1\>', $content);
	return $content;
}

function printruledebug($infoarr) {
	$rule = "";
	$html = '';
	$html .= showprogress('测试网页地址', 1);
	$html .= showprogress("<input type=\"text\" style=\"width: 95%\" value=\"" . $infoarr ['url'] . "\">");
	$html .= showprogress('网页源码', 1);
	$html .= showprogress(code(dhtmlspecialchars($infoarr ['source'])));
	$rule = dhtmlspecialchars($infoarr ['rule']);
	$html .= showprogress('正则表达式', 1);
	$html .= showprogress("<input type=\"text\" style=\"width: 95%\" value=\"" . $rule . "\">");
	if (is_array($infoarr ['code'])) {
		$infoarr ['code'] = implode("\n", $infoarr ['code']);
	}
	if (!empty($infoarr ['code'])) {
		$html .= showprogress('识别后有内容,区域源码', 1);
		$html .= showprogress(code($infoarr ['code']));
	} else {
		$html .= showprogress('没识别出任何内容,请检查识别规则', 1);
	}
	return $html;

	//exit( );
}

function showprogress($message, $title = 0) {
	if ($title) {
		return "<div class=\"progress\">" . $message . "</div>";
	} else {
		return $message . "<br>";
	}
}

function processurl($row) {
	$thl = trim($row ['titlehl']);
	if ($thl) {
		$thl = processrule($thl);
		$thl = str_replace("{keyword}", "([^']" . '[^"]' . "+)", $thl);
	}
	$chl = trim($row ['contenthl']);
	if ($chl) {
		$chl = processrule($chl);
		$chl = str_replace("{keyword}", "([^']" . '[^"]' . "+)", $chl);
	}
	if ($row ['format'] == 'http') {
		$sourcehtml = geturlfile($row ['url'], 1, $row ['charset']);
		if (!$sourcehtml)
			return false;
		preg_match("/<body(.*)<\/body>/isU", $sourcehtml, $matches);
		if ($matches [1])
			$sourcehtml = "<body" . $matches [1] . "</body>";
		$import = 'all';
		if (!$row ['rule']) {
			$import = 'content';
			$result = getrobotmessage($sourcehtml, $row ['url'], 1, 1);
			$result = processrepl($row ['replace'], $result);
		} else {
			$row ['rule'] = processrule($row ['rule']);
			preg_match_all('/\{([a-zA-Z:]+)\}/i', $row ['rule'], $matches);
			if ($matches [1]) {
				$i = 1;
				foreach ($matches [1] as $k => $v) {
					$v = strtolower($v);
					if ($v !== 'ignore') {
						$rules [$i] = $v;
						if ($v !== 'content') {
							$matches [1] [$k] = "([^'][^>]" . '[^"]' . "+)";
						} else {
							$import = 'content';
							$matches [1] [$k] = "(.*?)";
						}
						$i++;
					} else {
						$matches [1] [$k] = ".*?";
					}
				}
			}
			if ($import == 'all') {
				$sourcehtml = processrepl($row ['replace'], $sourcehtml);
			}
			$row ['rule'] = str_replace($matches [0], $matches [1], $row ['rule']);
			$row ['rule'] = str_replace('\\\\', '\\', $row ['rule']);
			//echo "<textarea rows=5 cols=100>".$row['rule']."</textarea>";
			//echo "<textarea rows=5 cols=100>".$sourcehtml."</textarea>";
			if ($import == 'all') {
				preg_match_all("~" . $row ['rule'] . "~iU", $sourcehtml, $matches);
			} else {
				preg_match("~" . $row ['rule'] . "~isU", $sourcehtml, $matches);
			}
			unset($matches [0]);
			if ($matches [1]) {
				if ($import == 'content') {
					foreach ($rules as $k => $v) {
						if ($v == 'content') {
							$result = processrepl($row ['replace'], $matches [$k]);
							$result = getrobotmessage($result, $row ['url'], 1, 0);
						}
					}
				} else {
					$results = array();
					$result = '';
					foreach ($matches as $k => $v) {
						if ($rules [$k]) {
							if (is_array($v)) {
								$formaturl = false;
								$type = $rules [$k];
								if (substr($type, - 4) == ':url') {
									$type = substr($type, 0, - 4);
									$formaturl = true;
								}

								$results [$type] = $v;
								$result .= $type . "��\n";
								foreach ($v as $i) {
									$i = trim($i);
									if ($rules [$k] == 'title' && $thl) {
										$i = preg_replace('/' . $thl . '/i', "\\1", $i);
									} elseif ($rules [$k] == 'content' && $chl) {
										$i = preg_replace('/' . $chl . '/i', "\\1", $i);
									}

									if ($formaturl) {
										$i = FillUrl($row ['url'], $i);
									}
									$result .= "\t$i\n";
								}
							} else {
								if ($rules [$k] == 'content') {
									$result = processrepl($row ['replace'], $v);
								}

								//$result = getrobotmessage($v,$row['url'],1,0);
							}
						}
					}
				}
			}
		}
	} else {
		require_once libfile('class/focus');
		$row ['rule'] = trim($row ['rule']);
		$lists = array("title" => lang('core', 'Title'), 'author' => lang('core', 'Author'), "content" => lang('core', 'Content'), "link" => lang('core', 'Link'));
		if ($row ['rule']) {
			$tmp = explode("\n", $row ['rule']);
			foreach ($tmp as $k => $v) {
				$v = strtolower($v);
				$v = explode("=", $v);
				$rules [trim($v [0])] = trim($v [1]);
			}
		}
		foreach ($lists as $k => $v) {
			if (!$rules [$k])
				$rules [$k] = $k;
		}
		$bl = new focus ( );
		$bl->rssURL = $row ['url'];
		$bl->readthreadlist("cancer");
		if ($bl->threadList) {
			$i = 1;
			foreach ($bl->threadList as $k) {
				$result .= "No. " . $i . "\n";
				foreach ($lists as $j => $v) {
					if ($k [$rules [$j]]) {
						$k [$rules [$j]] = htmlspecialchars_decode($k [$rules [$j]]);
						if ($j == 'title' && $thl) {
							$k [$rules [$j]] = preg_replace('/' . $thl . '/i', "\\1", $k [$rules [$j]]);
						}
						if ($j == 'content' && $chl) {
							$k [$rules [$j]] = preg_replace('/' . $chl . '/i', "\\1", $k [$rules [$j]]);
						}
						$result .= "\t" . $v . "==" . $k [$rules [$j]] . "\n";
					}
				}
				$result .= "\n=======================================================\n";
				$i++;
			}
		}
		ob_start ();
		print_r($bl->threadList);
		$sourcehtml = ob_get_contents ();
		ob_end_clean ();

		//		$sourcehtml = serialize($bl->threadList);
	}
	return array($result, $sourcehtml, $results);
}

function getimageurl($referurl, $subject) {
	preg_match_all("/<img([^>]+)>/isU", $subject, $imglist);
	$imglist = $imglist [0];
	if (is_array($imglist)) {
		foreach ($imglist as $k => $v) {
			$src = '';
			$str = $v;
			$v = trim(substr($v, 4, - 1));
			preg_match("/src=('|\")?(.*)(\\1)/ismUe", $v, $src);
			if ($src [2]) {
				$src = trim($src [2]);
			} else {
				$v = explode(' ', $v);
				if (is_array($v)) {
					foreach ($v as $vv) {
						$vv = explode('=', $vv);
						$vv [0] = strtolower($vv [0]);
						if ($vv [0] == 'src') {
							$src = trim($vv [1]);
							break;
						}
					}
				}
			}
			if ($src) {
				$imglist [$k] = array();
				$imglist [$k] [0] = $str;
				$imglist [$k] [1] = $src;
			} else {
				unset($imglist [$k]);
			}
		}
	}
	$offset = '';
	$url = $imagereplace = array();
	$posturl = parse_url($referurl);
	if (is_array($imglist) && !empty($imglist)) {
		foreach ($imglist as $tempvalue) {
			$url = FillUrl($referurl, $tempvalue [1]);
			$imagereplace ['newimageurl'] [] = '&lt;img src=' . $url . '&gt;';
			$imagereplace ['oldimageurl'] [] = $tempvalue [0];
		}
	}
	return str_replace($imagereplace ['oldimageurl'], $imagereplace ['newimageurl'], $subject);
}

function getpr($arraycell, $searchaborative, $replaceaborative) {
	$htmltags = array(array('title', 5), array('a', - 1), array('iframe', - 2), array('p', 1), array('li', - 1), array('input', - 0.1), array('select', - 3), array('form', - 0.1));

	if (strlen($arraycell ['text']) > 10) {
		if (strlen($arraycell ['text']) > 200) {
			$arraycell ['pr'] += 2;
		}

		foreach ($htmltags as $tagsvalue) {
			$temp = array();
			preg_match_all("/\<$tagsvalue[0][^\>]*?\>/is", $arraycell ['code'], $temp, PREG_SET_ORDER);
			$tagsnum = count($temp);

			$temp = array();
			if ($tagsvalue [0] == 'title' && $tagsnum > 0) {
				$arraycell ['title'] = 'title';
			} elseif ($tagsvalue [0] == 'a' && $tagsnum > 0) {
				preg_match_all("/\<a[^\>]*?\>(.*?)\<\/a>/is", $arraycell ['code'], $temp);
				$temp [2] = preg_replace("/[\n\r\s]*?/is", '', preg_replace($searchaborative, $replaceaborative, implode('', $temp [1])));
				$ahretnum = strlen($temp [2]) / strlen($arraycell ['text']);
				$tagsnum *= $ahretnum * 10;
			}

			$arraycell ['pr'] += $tagsnum * $tagsvalue [1];
		}
	} else {
		$arraycell ['pr'] -= 10;
	}

	if ($arraycell ['pr'] >= 0) {
		$g1 = preg_replace("/\<(p|br)[^\>]*?\>/si", "\n\n###p br explode###\n\n", $arraycell ['code']);
		$arrayg1 = explode("\n\n###p br explode###\n\n", $g1);

		preg_match_all("/\n\n###p br explode###\n\n/is", $g1, $g4, PREG_SET_ORDER);

		if (count($g4) > 2) {
			$g3 = 0;
			foreach ($arrayg1 as $value) {
				$g2 = preg_replace("/[\n\r\s]*?/is", "", preg_replace($searchaborative, $replaceaborative, $value));

				if ($g2 != '') {
					$g2num = strlen($g2);
					if ($g2num <= 25) {
						$g3--;
					} elseif ($g2num > 70) {
						$g3 = 10;
						continue;
					} else {
						$g3++;
					}
				}
			}

			if ($g3 < 0) {
				$arraycell ['pr'] += $g3;
			}
		}
	}

	return $arraycell;
}

function getrobotmessage($sourcehtml, $referurl, $robotlevel = 1) {
	$searchcursory = array("/\<(script|style|textarea)[^\>]*?\>.*?\<\/(\\1)\>/si", "/\<!*(--|doctype|html|head|meta|link|body)[^\>]*?\>/si", "/<\/(html|head|meta|link|body)\>/si", "/([\r\n])\s+/", "/\<(table|div)[^\>]*?\>/si", "/\<\/(table|div)\>/si");
	$replacecursory = array("", "", "", "\\1", "\n\n###table div explode###\n\n", "\n\n###table div explode###\n\n");
	$searchaborative = array("/\<(iframe)[^\>]*?\>.*?\<\/(\\1)\>/si", "/\<[\/\!]*?[^\<\>]*?\>/si", "/\t/", "/[\r\n]+/", "/(^[\r\n]|[\r\n]$)+/", "/&(quot|#34);/i", "/&(amp|#38);/i", "/&(lt|#60);/i", "/&(gt|#62);/i", "/&(nbsp|#160|\t);/i", "/&(iexcl|#161);/i", "/&(cent|#162);/i", "/&(pound|#163);/i", "/&(copy|#169);/i", "/&#(\d+);/e");
	$replaceaborative = array("", "", "", "\n", "", "\"", "&", "<", ">", " ", chr(161), chr(162), chr(163), chr(169), "chr(\\1)");
	$arrayrobotmeg = array();
	$sourcetext = replaceimageurl($referurl, preg_replace($searchcursory, $replacecursory, $sourcehtml));

	$arraysource = explode("\n\n###table div explode###\n\n", $sourcetext);
	$arraycell = array();
	foreach ($arraysource as $value) {
		$cell = array('code' => $value, 'text' => preg_replace("/[\n\r\s]*?/is", "", preg_replace($searchaborative, $replaceaborative, $value)), 'pr' => 0, 'title' => '', 'process' => '');
		if ($cell ['text'] != '') {
			if ($robotlevel == 2) {
				$arraycell [] = getpr($cell, $searchaborative, $replaceaborative);
			} else {
				$arraycell [] = $cell;
			}
		}
	}

	$arraysubject = $arraymessage = array();
	$leachsubject = $leachmessage = '';
	foreach ($arraycell as $value) {
		if ($value ['title'] == 'title') {
			$arraysubject [] = $value;
		} elseif ($value ['pr'] >= 0) {
			$arraymessage [] = $value ['code'];
		}
	}

	$pr = '';
	foreach ($arraysubject as $value) {
		if ($pr < $value ['pr'] || empty($pr)) {
			$leachsubject = $value ['text'];
		}
		$pr = $value ['pr'];
	}
	$leachmessage = preg_replace("/\<(p|br)[^\>]*?\>/si", "\n", implode("\n", $arraymessage));
	$arraymessage = explode("\n", preg_replace($searchaborative, $replaceaborative, $leachmessage));
	$leachmessage = '';
	foreach ($arraymessage as $value) {
		if (trim($value) != '') {
			$leachmessage .= "<p>" . trim($value) . "</p>\n";
		}
	}

	$arrayrobotmeg ['leachsubject'] = $leachsubject;
	$arrayrobotmeg ['leachmessage'] = $leachmessage;
	return $leachmessage;
	return $arrayrobotmeg;
}

function replaceimageurl($referurl, $subject) {
	preg_match_all("/<img.+\ssrc=('|\"|)?(.*)(\\1)([\s].*)?>/iU", $subject, $temp, PREG_SET_ORDER);
	$offset = '';
	$url = $imagereplace = array();
	$posturl = parse_url($referurl);
	if (is_array($temp) && !empty($temp)) {
		foreach ($temp as $tempvalue) {
			$url = parse_url(str_replace('\"', '', $tempvalue [2]));
			$imagereplace ['oldimageurl'] [] = $tempvalue [0];
			if (isset($url ['host']) && !empty($url ['host'])) {
				$imagereplace ['newimageurl'] [] = '&lt;img src="' . str_replace('\"', '', $tempvalue [2]) . '"&gt;';
			} else {
				$offset = strpos($tempvalue [2], '/');
				if (!is_bool($offset) && $offset == 0) {
					$imagereplace ['newimageurl'] [] = '&lt;img src="' . $posturl ['scheme'] . '://' . $posturl ['host'] . str_replace('\"', '', $tempvalue [2]) . '"&gt;';
				} else {
					$imagereplace ['newimageurl'] [] = '&lt;img src="' . substr($referurl, 0, strrpos($referurl, '/')) . '/' . str_replace('\"', '', $tempvalue [2]) . '"&gt;';
				}
			}
		}
	}

	return str_replace($imagereplace ['oldimageurl'], $imagereplace ['newimageurl'], $subject);
}

/**
 * 补全相对网址
 */
function FillUrl($url = '', $surl = '') {
	if ($url == "" || $surl = '')
		return;
	$urls = "";
	$urls = @parse_url($url);
	if (is_array($urls)) {
		$HomeUrl = $urls ["host"];
		$BaseUrlPath = $HomeUrl . $urls ["path"];
		$BaseUrlPath = preg_replace("/\/([^\/]*)\.(.*)$/", "/", $BaseUrlPath);
		$BaseUrlPath = preg_replace("/\/$/", "", $BaseUrlPath);
		$i = 0;
		$dstr = "";
		$pstr = "";
		$okurl = "";
		$pathStep = 0;
		$surl = trim($surl);
		if ($surl == "")
			return "";
		$pos = strpos($surl, "#");
		if ($pos > 0)
			$surl = substr($surl, 0, $pos);
		if ($surl [0] == "/") {
			$okurl = "http://" . $HomeUrl . $surl;
		} else if ($surl [0] == ".") {
			if (strlen($surl) <= 1)
				return "";
			else if ($surl [1] == "/") {
				$okurl = "http://" . $BaseUrlPath . "/" . substr($surl, 2, strlen($surl) - 2);
			} else {
				$urls = explode("/", $surl);
				foreach ($urls as $u) {
					if ($u == "..")
						$pathStep++;
					else if ($i < count($urls) - 1)
						$dstr .= $urls [$i] . "/";
					else
						$dstr .= $urls [$i];
					$i++;
				}
				$urls = explode("/", $BaseUrlPath);
				if (count($urls) <= $pathStep)
					return "";
				else {
					$pstr = "http://";
					for ($i = 0; $i < count($urls) - $pathStep; $i++) {
						$pstr .= $urls [$i] . "/";
					}
					$okurl = $pstr . $dstr;
				}
			}
		} else {
			if (strlen($surl) < 7)
				$okurl = "http://" . $BaseUrlPath . "/" . $surl;
			else if (strtolower(substr($surl, 0, 7)) == "http://")
				$okurl = $surl;
			else
				$okurl = "http://" . $BaseUrlPath . "/" . $surl;
		}
		$okurl = preg_replace("/^(http:\/\/)/i", "", $okurl);
		$okurl = preg_replace("/\/{1,}/i", "/", $okurl);
		return "http://" . $okurl;
	}
	return false;
}